
package com.platform.modules.mall.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.platform.common.annotation.SysLog;
import com.platform.common.dataImport.WhiteListImport;
import com.platform.common.utils.Constant;
import com.platform.common.utils.EasyPoiUtils;
import com.platform.common.utils.Md5Utils;
import com.platform.common.utils.RestResponse;
import com.platform.common.utils.dataImport.BaseImportExcel;
import com.platform.modules.mall.entity.MallWhitelistEntity;
import com.platform.modules.mall.service.*;
import com.platform.modules.sys.controller.AbstractController;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * Controller
 *
 * @author 
 * @date 2022-09-14 15:13:27
 */
@RestController
@RequestMapping("mall/whitelist")
@Slf4j
public class MallWhitelistController extends AbstractController {
    @Resource
    private MallWhitelistService mallWhitelistService;

    /**
     * 查看所有列表
     *
     * @param params 查询参数
     * @return RestResponse
     */
    @RequestMapping("/queryAll")
    @RequiresPermissions("mall:whitelist:list")
    public RestResponse queryAll(@RequestParam Map<String, Object> params) {
        List<MallWhitelistEntity> list = mallWhitelistService.queryAll(params);
        return RestResponse.success().put("list", list);
    }

    /**
     * 分页查询
     *
     * @param params 查询参数
     * @return RestResponse
     */
    @GetMapping("/list")
    @RequiresPermissions("mall:whitelist:list")
    public RestResponse list(@RequestParam Map<String, Object> params) {
        Page<MallWhitelistEntity> page = mallWhitelistService.queryPage(params);
        return RestResponse.success().put("page", page);
    }

    /**
     * 根据主键查询详情
     *
     * @param id 主键
     * @return RestResponse
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("mall:whitelist:info")
    public RestResponse info(@PathVariable("id") String id) {
        MallWhitelistEntity mallWhitelist = mallWhitelistService.getById(id);
        return RestResponse.success().put("whitelist", mallWhitelist);
    }

    /**
     * 新增
     *
     * @param mallWhitelist mallWhitelist
     * @return RestResponse
     */
    @SysLog("新增")
    @RequestMapping("/save")
    @RequiresPermissions("mall:whitelist:save")
    public RestResponse save(@RequestBody MallWhitelistEntity mallWhitelist) {
        mallWhitelistService.add(mallWhitelist);
        return RestResponse.success();
    }

    /**
     * 修改
     *
     * @param mallWhitelist mallWhitelist
     * @return RestResponse
     */
    @SysLog("修改")
    @RequestMapping("/update")
    @RequiresPermissions("mall:whitelist:update")
    public RestResponse update(@RequestBody MallWhitelistEntity mallWhitelist) {
        mallWhitelistService.update(mallWhitelist);
        return RestResponse.success();
    }

    /**
     * 根据主键删除
     *
     * @param ids ids
     * @return RestResponse
     */
    @SysLog("删除")
    @RequestMapping("/delete")
    @RequiresPermissions("mall:whitelist:delete")
    public RestResponse delete(@RequestBody String[] ids) {
        mallWhitelistService.deleteBatch(ids);
        return RestResponse.success();
    }

    /**
     * 上传文件
     *
     * @param file file
     * @return RestResponse
     */
    @RequestMapping("/upload")
    @RequiresPermissions("mall:whitelist:save")
    @Transactional(rollbackFor = Exception.class)
    public Object upload(@RequestParam(value = "file", required = false) MultipartFile file, Integer type,Integer isMd5,String lapseTime) throws Exception {
        if (type == null) {
            return RestResponse.error("白名单类型不能为空！");
        }
        if (StringUtils.isEmpty(lapseTime)) {
            return RestResponse.error("过期时间不能为空！");
        }
        BaseImportExcel importExcel = new WhiteListImport(mallWhitelistService, type,isMd5,lapseTime);
        EasyPoiUtils.importExcel(file, importExcel);
        return RestResponse.success();
    }

}
